import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_app/util/show.dart';
import 'package:get/get.dart';

import 'logic.dart';
import 'state.dart';

class snackbarPage extends StatelessWidget {
  final snackbarLogic logic = Get.put(snackbarLogic());
  final snackbarState state = Get.find<snackbarLogic>().state;

  @override
  Widget build(BuildContext context) {
    final user = User().obs;
    return Scaffold(
      appBar: AppBar(
        title: Text('111'),
        centerTitle: true,
      ),
      body: Center(
        child: Column(
          children: [
            ///简单式状态管理器：主动 性能高
            GetBuilder<snackbarLogic>(builder: (_) => Text('${logic.counter}')),
            GetBuilder<snackbarLogic>(

                ///如果你想用GetBuilder完善一个widget的更新控件，你可以给它们分配唯一的ID。
                id: 'text',
                builder: (_) => Text('${logic.counter}',style: TextStyle(fontSize: 30),)),

            ///响应式状态管理器：被动
            GetX<snackbarLogic>(
              builder: (controller) {
                print("count 1 rebuild");
                return Text('${controller.count1.value}');
              },
            ),
            GetX<snackbarLogic>(
              builder: (controller) {
                print("count 1 rebuild");
                return Text('${controller.count1.value}');
              },
            ),
            GetX<snackbarLogic>(
              builder: (controller) {
                print("count 2 rebuild");
                return Text('${controller.count2.value}');
              },
            ),
            GetX<snackbarLogic>(
              builder: (controller) {
                print("count 3 rebuild");
                return Text('${controller.sum}');
              },
            ),
            ElevatedButton(
                child: Text('bottomsheet'),
                onPressed: () {
                  ///底部弹框
                  Get.bottomSheet(
                      Container(
                        height: 300,
                        color: Colors.white,
                        child: ListView(
                          children: [
                            ListTile(
                                title: Text('11'),
                                leading:
                                    Icon(Icons.restaurant, color: Colors.red),
                                trailing: Icon(Icons.keyboard_arrow_right),
                                onTap: () {
                                  Get.back();
                                  showToast('11');
                                }),
                            ListTile(title: Text('22')),
                            ListTile(title: Text('33')),
                            ListTile(title: Text('44')),
                          ],
                        ),
                      ),
                      enableDrag: false,
                      isDismissible: false);
                }),
            ElevatedButton(
                child: Text('dialog'),
                onPressed: () {
                  ///对话框
                  Get.defaultDialog(
                      title: '通知',
                      titleStyle: TextStyle(color: Colors.red),
                      content: Column(
                        children: [Text('000'), Text('111'), Text('222')],
                      ),
                      buttonColor: Colors.blue,
                      textCancel: '取消',
                      cancelTextColor: Colors.blue,

                      ///取消事件
                      onCancel: () {
                        showToast('取消obs');
                      },

                      ///自定义按钮
                      cancel: RaisedButton(
                          color: Colors.grey,
                          textColor: Colors.white,
                          child: Text('取消'),
                          onPressed: () {
                            Get.back();
                          }),
                      confirm: RaisedButton(
                          color: Colors.blue,
                          child: Text('提交'),
                          onPressed: () {
                            Get.back();
                            Get.snackbar('title', 'message');
                          }),

                      ///点击外部不消失
                      barrierDismissible: false,
                      radius: 10
                      // textConfirm: '确定',
                      // confirmTextColor: Colors.white
                      );
                }),
            ElevatedButton(
                child: Text('snackbar'),
                onPressed: () {
                  Get.snackbar('000', '000',

                      ///字体颜色
                      colorText: Colors.white,

                      ///背景颜色
                      backgroundColor: Colors.black54,

                      ///显示时间
                      duration: Duration(milliseconds: 1500),

                      ///显示位置
                      snackPosition: SnackPosition.BOTTOM,

                      ///自定义文字样式
                      titleText: Text(
                        '新标题',
                        style: TextStyle(color: Colors.red),
                      ),
                      messageText: Column(
                        children: [
                          Icon(
                            Icons.add,
                            color: Colors.white,
                          ),
                          Icon(
                            Icons.add,
                            color: Colors.white,
                          ),
                          Icon(
                            Icons.add,
                            color: Colors.white,
                          ),
                        ],
                      ),

                      ///设置图标
                      icon: Icon(Icons.usb_rounded,
                          color: Colors.white, size: 35),

                      ///边框
                      borderWidth: 2.0,

                      ///边框颜色
                      borderColor: Colors.red);
                }),
            ElevatedButton(

                ///跳转页面传值
                child: Text('跳转页面'),
                onPressed: () {
                  Get.to(other(),
                      transition: Transition.zoom,
                      curve: Curves.bounceIn,
                      arguments: {'name': '000', 'age': '10'});
                }),
            ElevatedButton(

                ///跳转页面传值
                child: Text('跳转页面'),
                onPressed: () {
                  Get.toNamed('/other',
                      arguments: {'name': '000', 'age': '10'});
                }),

            ///GetX
            ElevatedButton(
                child: Text('响应式状态管理器GetX'),
                onPressed: () {
                  ///final是个对象要用value++
                  logic.count1.value++;
                }),
            // view
            Obx(() => Text("now count1 ${logic.count1}")),
            Obx(() => Text("Name ${user.value.name}: Age: ${user.value.age}")),
            ElevatedButton(
                child: Text('响应式状态管理器 obx update user'),
                onPressed: () {
                  user.update((user) {
                    user.name = 'Jonny';
                    user.age = 18;
                  });
                }),
            ElevatedButton(
                child: Text('响应式状态管理器 obx update user 2'),
                onPressed: () {
                  // 更新user变量的另一种方式。
                  user(User(name: 'João', age: 35));
                }),
            ElevatedButton(
                child: Text('简单式状态管理器GetBuilder'),
                onPressed: () {
                  logic.increment();
                }),
            ElevatedButton(
                child: Text('new page'),
                onPressed: () {
                  Get.to(NewPage());
                }),
          ],
        ),
      ),
    );
  }
}

class other extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final result = Get.arguments;
    return Scaffold(
      appBar: AppBar(
        leading: GestureDetector(
          onTap: () {
            Get.back();
          },
          child: Icon(Icons.arrow_back),
        ),
      ),
      body: Center(
        child: Text('${result}' + Get.arguments['name']),
      ),
    );
  }
}

class NewPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final controller = Get.find<snackbarLogic>();
    return Scaffold(
      appBar: AppBar(
        title: Text('NewPage'),
        centerTitle: true,
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Obx(() => Text(
                  controller.count1.toString(),
                  style: TextStyle(color: Colors.red, fontSize: 50),
                ))
          ],
        ),
      ),
    );
  }
}

class User {
  User({this.name = '', this.age = 0});

  String name;
  int age;
}
